﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaDeBastanteodePoderes.Negocio;
using SistemaDeBastanteodePoderes.Dominio;

namespace SistemaDeBastanteodePoderes.Controllers
{
    public class PoderController : Controller
    {
        //Dependencias
        public SupervisorServiceImpl SupervisorService { get; set; }

        // GET: /Poder/
        public ActionResult Index()
        {
            TempData["tipoServicio"] = ListarTipoServicio();
            ICollection<Poder> modelo = SupervisorService.ListarPoderes();
            return View(modelo);
        }

        //
        // GET: /Poder/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Poder/Create
        public ActionResult Create()
        {
            TempData["tipoServicio"] = ListarTipoServicio();
            return View();
        } 

        //
        // POST: /Poder/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    Poder clienteACrear = new Poder();
                    string CodCorto = (String)collection["CodCorto"];
                    string NomPoder = (String)collection["NomPoder"];
                    int tipServicioCodigo = int.Parse(collection["TipoServ"]);
                    TempData["tipoServicio"] = ListarTipoServicio();
                    SupervisorService.AgregarPoder(CodCorto, NomPoder, tipServicioCodigo);
                }
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                TempData["tipoServicio"] = ListarTipoServicio();
                return View("Create");
            }
        }
        
        //
        // GET: /Poder/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Poder/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Poder/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Poder/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        private ICollection<SelectListItem> ListarTipoServicio()
        {
            ICollection<SelectListItem> tipoServicio = new List<SelectListItem>();
            tipoServicio.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (TipoServicio tipo in SupervisorService.ListarTiposServicio())
                tipoServicio.Add(new SelectListItem()
                {
                    Value = tipo.Codigo.ToString(),
                    Text = tipo.Servicio
                });
            return tipoServicio;
        }

    }
}